package com.dream.testtubebaby.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.data.product.ShoppingCartItemData;
import com.dream.testtubebaby.utils.AppUtil;
import com.dream.testtubebaby.utils.ImageLoader;
import com.dream.testtubebaby.utils.ViewHolder;

public class ProductShoppingCartAdapter extends
		CommonAdapter<ShoppingCartItemData> {

	public interface onCheckedChanged {

		public void getChoiceData(ShoppingCartItemData item);
	}

	private onCheckedChanged listener;

	public void setOnCheckedChanged(onCheckedChanged listener) {
		this.listener = listener;
	}

	public ProductShoppingCartAdapter(Context context,
			List<ShoppingCartItemData> mDatas, int itemLayoutId) {
		super(context, mDatas, itemLayoutId);
	}

	@Override
	public void convert(ViewHolder helper, final ShoppingCartItemData item) {
		CheckBox shop_cart_item_choice = helper
				.getView(R.id.shop_cart_item_choice);
		if (item.getIconUrl() != null) {
			ImageLoader.getInstance().loadNetWorkImage(
					AppUtil.getCompletePhotoUrl(item.getIconUrl()),
					(ImageView)helper.getView(R.id.shop_cart_item_pic));
		}
		helper.setText(R.id.shop_cart_item_name, item.getProduct_name());
		if(!TextUtils.isEmpty(item.getProduct_dec())){
			helper.setText(R.id.shop_cart_item_dec, item.getProduct_dec());
		}
		helper.setText(R.id.shop_cart_item_price, String.valueOf(item.getProduct_price()));
		helper.setText(R.id.shop_cart_item_num, String.valueOf(item.getProduct_num()));
		shop_cart_item_choice.setChecked(item.isCheck());
		shop_cart_item_choice
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean choice) {
						item.setCheck(choice);
						listener.getChoiceData(item);
					}
				});
	}

}
